package cn.uyunsky.platform.config.factorybean;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

import cn.uyunsky.platform.config.ConfigBeanInterface;
import cn.uyunsky.platform.config.ConfigLoader;
import cn.uyunsky.platform.config.ConfigReader;

/**
 * conifg-*.xml中每个config节点对象
 *
 * @author baiyh
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class ConfigBean {

    private String bean;
    private String path;
    private String loader;
    private String reader;

    public String getBean() {
        return bean;
    }

    @SuppressWarnings("unchecked")
    public Class<? extends ConfigBeanInterface> getBeanClass() {
        try {
            return (Class<? extends ConfigBeanInterface>) Class.forName(bean);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setBean(String bean) {
        this.bean = bean;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getLoader() {
        return loader;
    }

    public ConfigLoader getLoaderInstance() {
        try {
            return (ConfigLoader) Class.forName(loader).newInstance();
        } catch (Exception e) {
            throw new RuntimeException("获取bean[" + bean + "]loader类失败", e);
        }
    }

    public void setLoader(String loader) {
        this.loader = loader;
    }

    public String getReader() {
        return reader;
    }

    @SuppressWarnings("unchecked")
    public ConfigReader<ConfigBeanInterface> getReaderInstance() {
        try {
            return ((ConfigReader<ConfigBeanInterface>) Class.forName(reader).newInstance());
        } catch (Exception e) {
            throw new RuntimeException("获取bean[" + bean + "]reader类失败", e);
        }
    }

    public void setReader(String reader) {
        this.reader = reader;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("ConfigBean [bean=");
        builder.append(bean);
        builder.append(", path=");
        builder.append(path);
        builder.append(", loader=");
        builder.append(loader);
        builder.append(", reader=");
        builder.append(reader);
        builder.append("]");
        return builder.toString();
    }

}
